package com.goods.controller.business;

import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import com.goods.controller.business.service.ProductService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductVO;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.Get;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "物资资料")
@RestController
public class ProductController {

    @Autowired
    ProductService productService;


    /**
     * 物资列表显示
     * @param
     * @return
     */
    @GetMapping("/business/product/findProductList")
    public ResponseBean findProductList(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(value = "categorys", required = false) String categorys,
                                        ProductVO productVO){
        buildCategory(categorys,productVO);
        PageVO<ProductVO> productList = productService.findProductList(pageNum, pageSize, productVO);
        return ResponseBean.success(productList);
    }

    private void buildCategory(String categorys, ProductVO productVO) {
        if(categorys != null && !"".equals(categorys)){
            String[] split = categorys.split(",");

            switch (split.length){
                case 1:
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    break;
                case 2:
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    productVO.setTwoCategoryId(Long.parseLong(split[1]));
                    break;
                case 3:
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    productVO.setTwoCategoryId(Long.parseLong(split[1]));
                    productVO.setThreeCategoryId(Long.parseLong(split[2]));
                    break;
            }
        }
    }

    /**
     * 添加物资
     * @param productVO
     * @return
     * @throws Exception
     */
    @PostMapping("business/product/add")
    public ResponseBean add(@RequestBody @Validated ProductVO productVO) throws Exception{

        if(productVO.getCategoryKeys().length != 3){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资需要三级分类");
        }
        productService.add(productVO);
        return ResponseBean.success();
    }

    /**
     * 编辑
     * @param id
     * @return
     */
    @GetMapping("/business/product/edit/{id}")
    public ResponseBean<ProductVO> edit(@PathVariable Long id){
        return ResponseBean.success(productService.edit(id));
    }

    /**
     * 修改
     * @param id
     * @param productVO
     * @return
     * @throws Exception
     */
    @PutMapping("/business/product/update/{id}")
    public ResponseBean update(@PathVariable Long id ,
                               @RequestBody ProductVO productVO) throws Exception{
        if(productVO.getCategoryKeys().length != 3){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资需要三级分类id");
        }
        productService.update(id,productVO);
        return ResponseBean.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/business/product/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws Exception{
        productService.delete(id);
        return ResponseBean.success();
    }

    /**
     * 放入回收站
     * @param id
     * @return
     */
    @PutMapping("/business/product/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) throws Exception{
        productService.remove(id);
        return ResponseBean.success();
    }

    /**
     * 回收站恢复
     * @param id
     * @return
     */
    @PutMapping("/business/product/back/{id}")
    public ResponseBean back(@PathVariable Long id) throws Exception{
        productService.back(id);
        return ResponseBean.success();
    }

    /**
     * 物资资料审核
     * @param id
     * @return
     * @throws Exception
     */
    @PutMapping("/business/product/publish/{id}")
    public ResponseBean publish(@PathVariable Long id) throws Exception{
        productService.publish(id);
        return ResponseBean.success();
    }


}
